﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Utils
{
    public class Image
    {
        public static string FromByteToString(byte[] value)
        {
            if (value != null)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    return ASCIIEncoding.ASCII.GetString(value, 0, value.Length);
                }
            }
            else return null;
        }

        public static byte[] FromStringToByte(string value)
        {
            if (value != null)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    return ASCIIEncoding.ASCII.GetBytes(value);
                }

            }
            else { return null; }
        }

        public static System.Drawing.Image FromByteToImage(byte[] value)
        {
            if (value != null)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    stream.Write(value, 0, value.Length);
                    return System.Drawing.Image.FromStream(stream, true);
                }
            }
            else { return null; }
        }

        public static byte[] FromImageToByte(System.Drawing.Image image)
        {
            if (image != null)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    return stream.ToArray();
                }
            }
            else { return null; }
        }
    }
}
